import deepMerge from "../function/deepMerge.js"

const Class = function(vm) {
	this.vm = vm
	this.mapper = {}
}

Class.prototype.access = function(key, route) {
	// 所用route统一入口
}

Class.prototype.toCondition = function(key, params, condition) {
	if (condition === true) {
		this.to(key, params)
	}
}

Class.prototype.to = function(key, params) {
	let item = this.mapper[key]
	if (!item) {
		throw new Error("路由信息异常")
	}

	let route = {}

	if (typeof item === "string") {
		route = {
			name: item,
			params: params || {}
		}
	} else if (typeof item === "object") {
		if (!item.name) {
			throw new Error("路由错误#请设置name属性")
		}

		params = deepMerge(item.params || {}, params || {})
		route = {
			name: item.name,
			params: params
		}
	} else {
		throw new Error("路由异常")
	}

	this.access(key, route)
}

Class.prototype.query = function(key, query) {
	/* var item = this.mapper[key]
	
	if (!item) {
		this.vm.$toast.center("路由错误#")
		return
	}
	
	if (typeof item === "string") {
		this.vm.$router.push({path: item, query: query || {}})
	}
	if (typeof item === "object") {
		if (!item.path) {
			this.vm.$toast.center("路由错误#请设置path属性")
			return
		}
		query = this.vm.$e.deepMerge(item.query || {}, query || {})
		this.vm.$router.push({path: item.path, query: query})
	} */
}

export default new Class()
